<?php
namespace Wap\Controller;

use Think\Controller;
use Think\Model;
use Think\Verify;
use User\Model\ActivityModel;
use User\Model\ActivityRegisterModel;
use Wap\Service\WeixinService;

//header('Access-Control-Allow-Origin: *');

class ActivityjlController extends WapController
{

    public $_activity;//活动信息
    public $wxuser;//活动绑定的微信号
    public $user;//微信用户
    public $is_mobile;

    public function __construct()
    {
        parent::__construct();

        if (!headers_sent() && // 如果页面头部信息还没有输出--
            extension_loaded("zlib") && // 而且php已经加载了zlib扩展--
            strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")
        ) //而且浏览器接受GZIP--
        {
            ini_set('zlib.output_compression', 'On');
            ini_set('zlib.output_compression_level', '4');
        }

        $this->handleCriteriaMember();
        $this->handleCriteriaWxuser();
        $this->is_mobile = $this->isMobile();
        if ($this->is_mobile) {
            $wxJSApi = WeixinService::buildWXJSApi($this->wxuser);
            /*if(empty($wxJSApi)){
                header("Content-type: text/html; charset=utf-8");
                die('服务器繁忙请稍后！');
            }*/
            $this->assign('nonceStr', $wxJSApi['noncestr']);
            $this->assign('wxTicket', $wxJSApi['ticket']);
            $this->assign('timestamp', $wxJSApi['timestamp']);
            $this->assign('wxSha1', $wxJSApi['sha1']);

            $this->assign("wxuser", $this->wxuser);
            $this->assign("id", $this->_activity['act_id']);
            $this->assign("imgurl", 'http://' . $_SERVER['HTTP_HOST'] . "/Public/images/jldm.jpg");
        }
    }

    /**
     *函数名: index
     *函数描述: 获取微信网页静默授权CODE、加载所有作品主页模板
     * @param string $_GET ['code'];
     * @return object 模板
     */
    public function index()
    {
        $activity = $this->_activity;
        $status = 1;//-1正常，3过期，2还未开始-
        if (empty($activity) || ($activity['end_date'] < date("Y-m-d H:i:s"))) {
            $status = 2;
        }
        if (!empty($activity) && $activity['start_date'] > date("Y-m-d H:i:s")) {
            $status = 3;
        }
        $this->assign("status", $status);
        return $this->display();
    }

    public function register()
    {
        $params = I("get.", "", "");
        if (empty($params['name'])) {
            return $this->ajaxReturn(array('status' => -1, "result" => "请填写名称"));
        }
        $verify = new Verify();
        if (!$verify->check(strtoupper($params['code']), '')) {
            $this->ajaxReturn(array("status" => -7, "result" => "验证码不正确" . strtoupper($params['code'])));
        }
        if (empty($params['id'])) {
            return $this->ajaxReturn(array('status' => -2, "result" => "请选择相应活动"));
        }
        if (empty($params['company'])) {
            return $this->ajaxReturn(array('status' => -3, "result" => "请填写公司名称(Please fill in the company name)"));
        }
        if (empty($params['mobile'])) {
            return $this->ajaxReturn(array('status' => -4, "result" => "请填写申请人手机号码(Please fill in the applicant's phone number)"));
        }
        if (empty($this->wxuser['token'])) {
            return $this->ajaxReturn(array('status' => -6, "result" => "注册项目有误(Registration item error)"));
        }
        if (empty($params['card_number'])) {
            return $this->ajaxReturn(array('status' => -10, "result" => "请填写证件号码(Please fill in the certificate number)"));
        }
        $cond['mobile'] = $params['mobile'];
        $cond['token'] = $this->wxuser['token'];
        $cond['act_id'] = $params['id'];
        $register = M("activity_register")->where($cond)->find();
        if (!empty($register)) {
            return $this->ajaxReturn(array('status' => -8, "result" => "该手机号码已被注册(The phone number has been registered)"));
        }
        $data['name'] = $params['name'];
        $data['token'] = $this->wxuser['token'];
        $data['act_id'] = $params['id'];
        $data['appellation'] = empty($params['appellation']) ? 0 : $params['appellation'];
        $data['company'] = $params['company'];
        $data['position'] = $params['position'];
        $data['industry'] = empty($params['industry']) ? 0 : $params['industry'];
        $data['address'] = $params['address'];
        $data['mobile'] = $params['mobile'];
        $data['note'] = $params['note'];
        $data['card_number'] = $params['card_number'];
        $data['status'] = ActivityRegisterModel::STATUS_PENDING;
        M("activity_register")->add($data);
        return $this->ajaxReturn(array('status' => 1,
            "result" => "您的报名主办方已收到。"));
    }

    /*
    *是否合法活动
    */
    private function handleCriteriaMember()
    {
        $cond['act_id'] = IS_GET ? I("get.id", -1, "") : I("post.id", -1, "");
        $cond['status'] = ActivityModel::STATUS_ACTIVE;
        $this->_activity = M("activity")->where($cond)->find();
        /*if (empty($this->_activity)) {
            $this->error("注册活动已过期", "#");
        }*/
    }

    /*
    *微信公众号信息
    */
    private function handleCriteriaWxuser()
    {
        $activity = $this->_activity;
        if (isset($activity['token'])) {
            $this->wxuser = WeixinService::getWxuser($this->_activity['token']);
        }
    }

    public function getVerify()
    {
        $verify = new Verify();
        $verify->entry();
    }
}
